<?php
	include_once("./global.php");
	session_start();
	if(isset($_POST['delete_tag'])){
		$delete_arr = $_POST['tags_kkid'];
		foreach($delete_arr as $key => $value){
			$value = $delete_arr[$key];
			$sql_delete = "update keywordinfo set K_tags = '0' where K_KID = '$value'";
			$query_delete = $mysqli->query($sql_delete);
			if($query_delete){
				echo "<script>alert('标签删除成功')</script>";
			}else{
				echo "<script>alert('标签删除失败')</script>";
				echo $mysqli->error;
			}
		}
	}
	
	if(isset($_POST['insert_tags'])){
		$sql_select_tags = "select K_KID from keywordinfo where K_Name = '$_POST[insert_name]'";
		$query_select_tags = $mysqli->query($sql_select_tags);
		$result_select_tags = $query_select_tags->fetch_assoc();
		if($result_select_tags['K_KID']){
			$tagkid = $result_select_tags['K_KID'];
			$sql_update_tags = "update keywordinfo set K_tags = '1' where K_KID = '$tagkid'";
			$query_update_tags = $mysqli->query($sql_update_tags);
			if($query_update_tags){
				echo "<script>alert('标签添加成功')</script>";
			}else{
				echo "<script>alert('标签添加失败‘)</script>";
			}
		}else{
			$sql_insert = "insert into keywordinfo (K_KID,K_ID,K_Name,K_search,K_tags) values (NULL,NULL,'$_POST[insert_name]',NULL,'1')";
			$query_insert = $mysqli->query($sql_insert);
			if($query_insert){
				echo "<script>alert('标签添加成功')</script>";
			}else{
				echo "<script>alert('标签添加失败‘)</script>";
			}
		}
	}
	


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>后台首页</title>
<style>
	body{ background:#e7e7e7; margin:0; padding:0;}
	#container{ width:980px; height:auto; position:relative; margin:0 auto;}
	#container #header{ width:980px; height:90px; position:relative; background:#000; line-height:90px;color:#fff; font-size:24px; text-align:center;}
	.left{width:250px; height:auto; position:relative; float:left;}
	.left table{width:248px; height:auto; position:relative; float:left; background:#fff;}
	.left table td{ height:20px; text-align:center;}
	.left table .selected{ background:#73abe3;}
	#content{ width:730px; height:100%; position:relative; float:left; margin:2px 0px;}
	#content .title{ width:720px; height:25px; position:relative; float:left; line-height:25px; font-size:18px; color:#fff; background:#000;}
	form{margin:20px auto; width:720px; height:auto; position:relative; float:left;}
	form .inpuarea{ width:360px; height:150px;}
</style>
</head>
<body>
	<div id="container">
		<div id="header">
			818苹果站后台管理
		</div>
		<div class="left">
			<table>
				<tr><td style="background:#000000; color:#fff;">站点管理</td></tr>
				<tr><td><a href="admin_site_mangerment.php">全局管理</a></td></tr>
				<tr><td>静态页面管理</td></tr>
				<tr><td><a href="admin_link_mangerment.php?page=1">友情链接管理</a></td></tr>
				<tr><td><a href="admin_member_mangerment.php?page=1">会员管理</a></td></tr>
				<tr><td class="selected"><a href="admin_tags_mangerment.php?page=1">标签管理</a></td></tr>
				<tr><td><a href="admin_search_mangerment.php">搜索管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">文章管理</td></tr>
				<tr><td><a href="admin_category_mangerment.php">文章分类管理</a></td></tr>
				<tr><td><a href="admin_news_add.php">发布文章</a></td></tr>
				<tr><td><a href="admin_news_mangerment.php?page=1">文章管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">应用管理</td></tr>
				<tr><td><a href="admin_appcate_mangerment.php">应用分类管理</a></td></tr>
				<tr><td><a href="admin_app_add.php">添加应用</a></td></tr>
				<tr><td><a href="admin_app_mangerment.php?page=1">应用管理</a></td></tr>
				<tr><td><a href="admin_app_collection.php">应用采集</a></td></tr>
				<tr><td><a href="admin_app_collection_single.php">单应用采集</a></td></tr>
			</table>
		</div>
	<div id="content">
		<div class="title">标签管理</div>
		<form action="" method="post">
			<table>
				<tr>添加标签</tr>
				<tr><td><label>添加标签名称</label></td><td><input type="text" name="insert_name" /></td><td><input type="submit" name="check_tags" value="检查标签是否存在" /></td></tr>
				<tr><td></td>
				<?php
					if(isset($_POST['check_tags'])){
						$sql_check = "select K_KID,K_tags from keywordinfo where K_Name = '$_POST[insert_name]'";
						$query_check = $mysqli->query($sql_check);
						$result_check = $query_check->fetch_assoc();
						if($result_check['K_KID'] && $result_check['K_tags']){
				?>
							<td><?php echo $tag;?>该关键词已经是标签了，换一个吧</td>
            	<?php
						}else if($result_check['K_KID'] && !$result_check['K_tags']){
				?>
							<td><?php echo $tag;?>该关键词已存在，先可点击添加按钮，使其成为标签</td>
            	<?php
						}else if(!$result_check['K_KID'] && !$result_check['K_tags']){
				?>
							<td><?php echo $tag;?>该关键词不存在与关键词表，可添加为关键词并使其成为标签</td>
				<?php
						}
					}
				?></td><td></td></tr>
				<tr><td><input type="submit" name="insert_tags" value="添加" /></td></tr>
			</table>		
		</form>
		<form name="form1" action="" method="post">
		<table>
			<tr>标签列表</tr>
			<tr>
				<td style="width:70px;">标签编号</td>
                <td style="width:150px">标签iTunes编号</td>
				<td style="width:100px;">标签名称</td>
                <td>操作</td>
			</tr>
			<tr>
            <?php
				$sql_select = "select * from keywordinfo where K_tags = '1'";
				$query_select = $mysqli->query($sql_select);
				$total = $query_select->num_rows;
				$num = 25;
				$page = new Page($total,$num);
				
				$sql_tag = $sql_select."{$page->limit_start()}";
				$query_tag = $mysqli->query($sql_tag);
				while($result_tag = $query_tag->fetch_assoc()){
			?>
				<td style="width:70px;">
					<input type="checkbox" name="tags_kkid[]" value="<?php echo $result_tag['K_KID']?>"><?php echo $result_tag['K_KID']?>
				</td>
				<td style="width:100px;">
					<?php echo $result_tag['K_ID']?>
				</td>
                <td style="width:100px;">
					<?php echo $result_tag['K_Name']?>
				</td>
                <td><a href="admin_tag_update.php?id=<?php echo $result_tag['K_KID']?>">修改</a></td>
			</tr>
            <?php					
				}				
			?>
            <tr>
            	    <?php echo $page->pageFirst();?>

                	<?php echo $page->pagePre();?>

                	<?php echo $page->pageList(); ?>

                	<?php echo $page->pageNext();?>

                	<?php echo $page->pageLast();?>

					<?php echo $page->pageTotal();?>  
            </tr>
			<tr>
				<td>
					<input type="checkbox" id="quan" name="quan" onclick="select_all()" />全选
                    <input type="submit" name="delete_tag" value="删除" onclick="del()" />
				</td>
			</tr>
		</table>
		</form>
	</div>
    </div>
</body>
<script>
	function select_all(){
		var length = document.form1.elements.length;
		var i = 0;
		for(i=0;i<length;i++){
			var checked = document.form1.elements[i];
			if(checked.name != 'quan'){
				if(document.getElementById("quan").checked != false){
					checked.checked = true;
				}else{
					checked.checked = false;
				}
			}
		}
	}
	function del(){
		var length = document.form1.elements.length;
		var i = 0;
		for(i=0;i<length;i++){
			var checked = document.form1.elements[i];
			if(checked.name != 'quan'){
				if(checked.checked == true){
					alert(checked.name+checked.value);
				}
			}
		}
	}
</script>
</html>